//
//  AppDelegate+SKNet.m
//  skhr
//
//  Created by sujeking on 2018/9/18.
//  Copyright © 2018年 sujeking. All rights reserved.
//

#import "AppDelegate+ZCNet.h"
#import <CoreTelephony/CTCellularData.h>
#import <AFNetworking/AFNetworkReachabilityManager.h>
#import "AppAndDeviceTool.h"

@implementation AppDelegate (ZCNet)

- (void)configNetStatus:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //1.获取网络权限 根绝权限进行人机交互
    if (__IPHONE_10_0) {
        [self networkStatus:application didFinishLaunchingWithOptions:launchOptions];
    }else {
//        //2.2已经开启网络权限 监听网络状态
        [self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
    }
}

- (void)networkStatus:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //2.根据权限执行相应的交互
    CTCellularData *cellularData = [[CTCellularData alloc] init];
    
    /*
     此函数会在网络权限改变时再次调用
     */
    cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
        switch (state) {
            case kCTCellularDataRestricted:{
                NSLog(@"Restricted");
                //2.1权限关闭的情况下 再次请求网络数据会弹出设置网络提示
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"] options:NSDataReadingUncached error:nil];
                if (data) {
                    NSLog(@"网络通了");
                }
            }
                break;
            case kCTCellularDataNotRestricted: {
                NSLog(@"NotRestricted");
                //2.2已经开启网络权限 监听网络状态
                [self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
            }
                break;
            case kCTCellularDataRestrictedStateUnknown: {
                NSLog(@"Unknown");
                //2.3未知情况 （还没有遇到推测是有网络但是连接不正常的情况下）
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.skhrs.cn/login/getLoginSid"] options:NSDataReadingUncached error:nil];
                if (data) {
                    NSLog(@"网络通了");
                }
            }
                break;
            default:
                break;
        }
    };
}

/**
 实时检查当前网络状态
 */
- (void)addReachabilityManager:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    AppAndDeviceTool *tool = [AppAndDeviceTool shareInstance];
    AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:{
                NSLog(@"没有网络");
                tool.connectionTypeStr = @"NETWORK_NONE";
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                NSLog(@"网络通过WIFI连接：%@",@(status));
                tool.connectionTypeStr = @"NETWORK_WIFI";
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                NSLog(@"网络通过无线连接：%@",@(status) );
                tool.connectionTypeStr = @"NETWORK_WWAN";
                break;
            }
            default:
                break;
        }
    }];
    [afNetworkReachabilityManager startMonitoring];  //开启网络监视器；
}


@end
